package com.epsoft.odin7cj.flow.api.instance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import org.activiti.engine.HistoryService;
//import org.activiti.engine.RuntimeService;
//import org.activiti.engine.history.HistoricProcessInstance;
//import org.activiti.engine.history.HistoricVariableInstance;
//import org.activiti.engine.runtime.ProcessInstance;

import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.history.HistoricVariableInstance;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.epsoft.odin7cj.core.dto.RetData;
import com.epsoft.odin7cj.flow.bpo.instance.InstanceBPO;
import com.epsoft.odin7cj.flow.model.instance.FlowInstance;

/**
 * DESC: 流程实例
 * @author 胡义振  
 * @date 2019年12月16日
 */
@RestController
@RequestMapping("/api/flow/instance")
public class InstanceApi {
	
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private HistoryService historyService;
	@Autowired
	private InstanceBPO instanceBPO;
	
    /**
     * DESC: 启动流程实例
     * @param riskAuditFlowParams  
     * @return
     * @throws Exception
     */
	@RequestMapping("/createInstance")
    public RetData<String> createInstance(HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("\n 开始启动流程实例："+request.getParameter("fdId"));
		RetData<String> retData = new RetData<String>();
		String fdId = request.getParameter("fdId");
		// 启动实例
		runtimeService.startProcessInstanceById(fdId);
		System.out.println("\n 流程实例启动成功");
    	retData.setRetBody("流程启动成功...");
    	return retData;
    }
	
    /**
     * DESC: 获取所有运行中的实例
     * @return
     * @throws Exception
     */
	@RequestMapping("/getProcInstance")
    public RetData<List<FlowInstance>> getAllInstance() throws Exception {
		RetData<List<FlowInstance>> retData = new RetData<List<FlowInstance>>();
		List<FlowInstance> listFlowInstance = new ArrayList<FlowInstance>();
		List<ProcessInstance> listProIns = runtimeService.createProcessInstanceQuery().list();
		for(ProcessInstance proIns : listProIns) {
			listFlowInstance.add(instanceBPO.convertToInstance(proIns));
		}
    	retData.setRetBody(listFlowInstance);
    	return retData;
    }
	
    /**
     * DESC: 获取所有历史实例
     * @return
     * @throws Exception
     */
	@RequestMapping("/getHisInstance")
    public RetData<List<FlowInstance>> getHisInstance() throws Exception {
		RetData<List<FlowInstance>> retData = new RetData<List<FlowInstance>>();
		List<HistoricProcessInstance> listProIns = historyService.createHistoricProcessInstanceQuery().orderByProcessInstanceStartTime().desc().list();
		List<FlowInstance> listAudProIns = new ArrayList<FlowInstance>();
		
		for(HistoricProcessInstance proIns : listProIns) {
			FlowInstance flowInstance = new FlowInstance();
			Map<String, Object> instanceParams = new HashMap<String, Object>();
			List<HistoricVariableInstance> hviList = historyService.createHistoricVariableInstanceQuery().processInstanceId(proIns.getId()).list();
			for(HistoricVariableInstance hvi : hviList) {
				instanceParams.put(hvi.getVariableName(), hvi.getValue());
			}
		}
    	retData.setRetBody(listAudProIns);
    	return retData;
    }
	
    /**
     * DESC: 删除流程实例
     * @param instanceId  实例ID
     * @return
     * @throws Exception
     */
	@RequestMapping("/doDelInstance")
    public RetData<String> doDelInstance(String instanceId) throws Exception {
		RetData<String> retData = new RetData<String>();
		ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(instanceId).singleResult();
		if(pi==null){
			//该流程实例已经完成了
		    historyService.deleteHistoricProcessInstance(instanceId);
		}else{
			//该流程实例未结束的
		    runtimeService.deleteProcessInstance(instanceId,"");
		    historyService.deleteHistoricProcessInstance(instanceId);
		}
    	retData.setRetBody("");
    	return retData;
    }
	
}
